package com.example.controller.app;

import com.example.config.CommentMQConfig;
import com.example.config.RedisUtil;
import com.example.result.CommonResult;
import com.example.service.appservice.AppForumService;
import com.example.utils.MyOSSUtil;
import com.example.vo.CommentVO;
import com.example.vo.PostsVO;
import com.example.vo.RentalVO;
import com.example.vo.ReportVO;
import lombok.extern.log4j.Log4j2;
import org.junit.experimental.theories.ParametersSuppliedBy;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @BelongsProject："smart-property"
 * @BelongsPackage："com.example.controller.app"
 * @CreateTime："2022-11-17 13:36"
 * @Description：前端论坛
 * @Author：LK
 */
@Log4j2
@RestController
@RequestMapping("/appForumController")
public class AppForumController {

    @Autowired
    private AppForumService appForumService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * @description:全查帖子相关信息
     * @author: lk
     * @date: 2022/11/17 14:01
     * @return: com.example.result.CommonResult
     **/
    @PostMapping("/postsCheck")
    public CommonResult postsCheck() {
        // 从redis里去除全查数据
        if(redisUtil.get("postsRedisCheck") != null){
            return CommonResult.ok(redisUtil.get("postsRedisCheck"));
        }else{
            return CommonResult.fail();
        }
    }

    /**
     * @description:浏览量，自增
     * @author: lk
     * @date: 2022/11/17 16:25
     **/
    @PostMapping("/redisAutoincrement/{postsNumber}")
    public void redisAutoincrement(@PathVariable String postsNumber){
        // 判断redis里有没有以这个编号为key的集合
        Object liulanliang = redisUtil.hget("liulanliang", postsNumber);
        if(liulanliang == null){
            // 没有 创建
            redisUtil.hset("liulanliang",postsNumber, 1);
        }else{
            // 有 自增
            redisUtil.hincr("liulanliang",postsNumber, 1);
        }
    }

    /**
     * @description:发送评论
     * @author: lk
     * @date: 2022/11/20 21:00
     * @param: commentVO
     * @return: com.example.result.CommonResult
     **/
    @PostMapping("/sendComments")
    public CommonResult sendComments(@RequestBody CommentVO commentVO){
        try {
            rabbitTemplate.convertAndSend(
                    CommentMQConfig.SMART_JIAOHUANJI_NAME,
                    "pinglun1",
                    commentVO);
            return CommonResult.ok();
        }catch (Exception e){
            log.info("评论发送失败原因：" + e);
            return CommonResult.fail();
        }
    }

    /**
     * @description:把文件上传到oss
     * @author: lk
     * @date: 2022/11/21 16:35
     * @param: file
     * @return: com.example.result.CommonResult
     **/
    @PostMapping("/upFile")
    public String upFile(@RequestParam MultipartFile file){
        return new MyOSSUtil().uploadFile(file);
    }

    /**
     * @description:举报贴子
     * @author: lk
     * @date: 2022/11/21 20:12
     * @param: reportVO
     * @return: com.example.result.CommonResult
     **/
    @PostMapping("/report1")
    public CommonResult report1(@RequestBody ReportVO reportVO){
        return appForumService.report1(reportVO);
    }

    /**
     * @description:查询父评论
     * @author: lk
     * @date: 2022/11/22 10:26
     * @return: com.example.result.CommonResult
     **/
    @PostMapping("/commentFather/{postsNumber}")
    public CommonResult commentFather(@PathVariable String postsNumber) {
        return appForumService.commentFather(postsNumber);
    }

    /**
     * @description:发布帖子
     * @author: lk
     * @date: 2022/11/22 15:49
     * @param: postsVO
     * @return: com.example.result.CommonResult
     **/
    @PostMapping("/release")
    public CommonResult release(@RequestBody PostsVO postsVO){
        return appForumService.release(postsVO);
    }
    
    /**
     * @description:查询房子信息
     * @author: lk
     * @date: 2022/11/23 10:19
     * @return: com.example.result.CommonResult
     **/
    @PostMapping("/checkRabtal")
    public CommonResult checkRabtal() {
        return appForumService.checkRabtal();
    }

    /**
     * @description:发布租房
     * @author: lk
     * @date: 2022/11/24 9:49
     * @param: rentalVO
     * @return: com.example.result.CommonResult
     **/
    @PostMapping("/fabuRental")
    public CommonResult fabuRental(@RequestBody RentalVO rentalVO){
        return appForumService.fabuRental(rentalVO);
    }

    /**
     * @description:全查房屋
     * @author: lk
     * @date: 2022/11/24 10:53
     * @return: com.example.result.CommonResult
     **/
    @PostMapping("/floorAll")
    public CommonResult floorAll(){
        return appForumService.floorAll();
    }
}
